

class Chef::Recipe
include Orchestrate
end 
zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'

cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]
dbhost=""
search(:node, "deployment:#{deployment}").each do |anode|
	if anode.role?("om_runtdb2")
		dbhost = anode[:fqdn]
	end
end
jmshost=""
search(:node, "deployment:#{deployment}").each do |anode|
    if anode.role?("om_mq")
        jmshost = anode[:fqdn]
    end
end


include_recipe "liberty::default"

template "/tmp/jvm.options" do
    source "jvm.options.erb"
    owner "root"
    variables  :yfs_local => node[:om][:yfs_local] 
	action :create
end

template "/tmp/server.xml" do
    source "server.xml.erb"
    owner "root"
    variables  :yfs_local => node[:om][:yfs_local] 
	action :create
end


template "/etc/init.d/libertyservice" do
	source "libertyservice.erb"
	owner "root"
	mode 0755
	variables(
		:javadir => node[:java7][:installDir],		
		:libertydir => node[:liberty][:wlpDir]
	)
	action :create
end

directory "/migration" do
  user cuser
  group cgroup
  action :create
end


#create the migration shell script from template and put the script in /migration folder
template "/migration/appserver.sh" do
  source "liberty.sh.erb"
  backup false
  owner cuser
  group cgroup
  mode 0755
  variables(
  			:jmshost=>jmshost,
  			:dbhost=>dbhost,
			:serverdir=>node[:liberty][:defaultServerDir]
  )
  action :create
end

#create the migration shell script from template and put the script in /migration folder to enable SSL
template "/migration/enableSSL.sh" do
  source "enableSSL.sh.erb"
  backup false
  owner cuser
  group cgroup
  mode 0755
  action :create
end

include_recipe "common::setup_wasuser"
include_recipe "om::customer_overrides"


bash "Create the defaultServer..." do
	user "root"
	cwd "#{node[:liberty][:wlpDir]}"
	code <<-EOH
		export JAVA_HOME=#{node[:java7][:installDir]}
		export PATH=$JAVA_HOME/bin:$PATH
		cd #{node[:liberty][:wlpDir]}
		bin/server create defaultServer
		cp /tmp/jvm.options #{node[:liberty][:defaultServerDir]}
		cp /tmp/server.xml #{node[:liberty][:defaultServerDir]}
	EOH
end

# not sure why we are creating war files... commenting out the belwo...
# 
#warfiles=nil
#warfiles.each() do| warfile|
#	ruby_block "Wait for war file #{warfile} to get uploaded to the repository..." do
 #   	block do
#			i_url = "/deployments/" + deployment + "/config/om/runtime/application/war/" + warfile + "/status"
 #       	dep = Dependency.new(i_url, node[:client][:status], zkep)
  #      	dep.validate!
   # 	end
	#end
	#bash "Download #{warfile}.war" do
	#	user cuser
	#	group group
	#	cwd "/tmp"
	#	code <<-EOH
     #   	scp #{node[:common][:scp_repo]}/generated/#{deployment}/#{warfile}.war  /tmp
	#		chmod 755 /tmp/#{warfile}.war
    #	EOH
	#end
	#bash "Install #{warfile}.war into liberty dropins" do
	#	user "root"
	#	cwd "/tmp"
	#	code <<-EOH
	#		cp /tmp/#{warfile}.war #{node[:liberty][:installDir]}/wlp/usr/servers/defaultServer/dropins
    #	EOH
	#end
#end


ruby_block "Checking for ear build completion in ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/application/ear/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.validate!
    end
end

bash "Download EAR from the file server" do
    user cuser
    group cgroup
    cwd "/tmp/"
    code <<-EOH
        scp #{node[:common][:scp_repo]}/generated/#{deployment}/#{node[:common][:earName]} /tmp/#{node[:common][:earName]}
	chmod -R 755 /tmp/runtime.ear
    EOH
end

bash "Install properties.jar into local properties dir" do
	user cuser
	cwd "/tmp"
	code <<-EOH
		unzip -oq /tmp/#{node[:common][:earName]} properties.jar
		sudo mkdir -p #{node['om']['yfs_local']}/properties
		sudo unzip -oq /tmp/properties.jar -d #{node[:om][:yfs_local]}/properties
		sudo cp /tmp/customer_overrides.properties #{node[:om][:yfs_local]}/properties		
		sudo sed -i '/runtime/local/' #{node[:om][:yfs_local]}/properties/servers.properties
		sudo chmod -R a+rw  #{node['om']['yfs_local']}
	EOH
end
bash "Download File Bindings for MQ from the file server" do
    user cuser
    group cgroup
    cwd "/tmp/"
    code <<-EOH
		scp #{node[:common][:scp_repo]}/generated/#{deployment}/OM_JNDI/.bindings /tmp
		chmod -R 755 /tmp/.bindings
    EOH
end
bash "Copy file bindings" do
    user cuser
    group cgroup
    cwd "/tmp/"
    code <<-EOH
		sudo mkdir -p #{node[:om][:installDir]}/OM_JNDI
		sudo cp /tmp/.bindings #{node[:om][:installDir]}/OM_JNDI
	EOH
end

bash "Deploy EAR to Application Server." do
	user "root"	
	code <<-EOH
		cp /tmp/#{node[:common][:earName]} #{node[:liberty][:dropInDir]}
	EOH
end

#Added by CDL to store CA database to store SSL key 20141223
bash "Create security directory..." do
  user "root"
  cwd "#{node[:liberty][:wlpDir]}"
  code <<-EOH
     sudo mkdir -p #{node[:liberty][:defaultServerDir]}/resources/security
  EOH
end

#install newrelic java agent for liberty server
include_recipe "newrelic::install_libertyagent"

#install and configure logstash for app server after app install.
include_recipe "logstash::config_for_app"


